package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;


    /**
     * 获取所有物资去处
     */
    @Override
    public PageVO<Consumer> findConsumerList(int pageNum, int pageSize, String name) {
        //查询集合对象
        //判断条件
        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andEqualTo("name",name);
        }
        //查询
        List<Consumer> consumers = consumerMapper.selectByExample(example);
        if (consumers == null)
            return null;

        //构建分页条件
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Consumer> pageInfo = new PageInfo<>(consumers);
        //返回
        return new PageVO<Consumer>(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 添加物资去处
     * @param consumer
     */
    @Override
    public void add(Consumer consumer) {
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
    }


    /**
     * 编辑回显
     * @param id
     * @return
     */
    @Override
    public Consumer edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        return consumer;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }


    /**
     * 编辑
     * @param consumer
     */
    @Override
    public void update(Consumer consumer) {
        consumerMapper.updateByPrimaryKey(consumer);
    }


    /**
     * 查询所有去向
     * @return
     */
    @Override
    public List<Consumer> findAll() {
        return consumerMapper.selectAll();
    }
}
